from django.test import TestCase
from django.contrib.auth import get_user_model
from rest_framework.test import APITestCase
from rest_framework import status
from .models import Goods

User = get_user_model()


class GoodsPermissionTestCase(APITestCase):
    def setUp(self):
        # 创建货主用户
        self.shipper_user = User.objects.create_user(
            phone='13800138000',
            password='testpass123',
            role=User.Role.SHIPPER,
            role_authstatus=True
        )
        
        # 创建未认证的货主用户
        self.unverified_shipper_user = User.objects.create_user(
            phone='13800138001',
            password='testpass123',
            role=User.Role.SHIPPER,
            role_authstatus=False
        )
        
        # 创建车主用户
        self.driver_user = User.objects.create_user(
            phone='13800138002',
            password='testpass123',
            role=User.Role.DRIVER,
            role_authstatus=True
        )
        
        # 创建管理员用户
        self.admin_user = User.objects.create_user(
            phone='13800138003',
            password='testpass123',
            role=User.Role.ADMIN,
            role_authstatus=True
        )
        
        # 创建货源
        self.goods = Goods.objects.create(
            user=self.shipper_user,
            title='测试货源',
            weight=100.00,
            from_location='广州',
            to_location='深圳',
            loading_time='2025-10-25T08:00:00Z'
        )
    
    def test_verified_shipper_can_update_own_goods(self):
        """测试已认证的货主可以更新自己的货源"""
        self.client.force_authenticate(user=self.shipper_user)
        
        data = {
            'title': '更新后的货源',
            'weight': '150.00',
            'from_location': '广州天河',
            'to_location': '深圳南山',
            'loading_time': '2025-10-26T08:00:00Z'
        }
        
        response = self.client.put(f'/api/goods/{self.goods.id}/', data, format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        
        # 验证数据已更新
        self.goods.refresh_from_db()
        self.assertEqual(self.goods.title, '更新后的货源')
        self.assertEqual(str(self.goods.weight), '150.00')
    
    def test_unverified_shipper_cannot_update_goods(self):
        """测试未认证的货主不能更新货源"""
        self.client.force_authenticate(user=self.unverified_shipper_user)
        
        data = {
            'title': '更新后的货源',
            'weight': '150.00',
            'from_location': '广州天河',
            'to_location': '深圳南山',
            'loading_time': '2025-10-26T08:00:00Z'
        }
        
        response = self.client.put(f'/api/goods/{self.goods.id}/', data, format='json')
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
    
    def test_driver_cannot_update_goods(self):
        """测试车主不能更新货源"""
        self.client.force_authenticate(user=self.driver_user)
        
        data = {
            'title': '更新后的货源',
            'weight': '150.00',
            'from_location': '广州天河',
            'to_location': '深圳南山',
            'loading_time': '2025-10-26T08:00:00Z'
        }
        
        response = self.client.put(f'/api/goods/{self.goods.id}/', data, format='json')
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
    
    def test_admin_cannot_update_goods(self):
        """测试管理员不能更新货源"""
        self.client.force_authenticate(user=self.admin_user)
        
        data = {
            'title': '更新后的货源',
            'weight': '150.00',
            'from_location': '广州天河',
            'to_location': '深圳南山',
            'loading_time': '2025-10-26T08:00:00Z'
        }
        
        response = self.client.put(f'/api/goods/{self.goods.id}/', data, format='json')
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
    
    def test_other_shipper_cannot_update_goods(self):
        """测试其他货主不能更新别人的货源"""
        # 创建另一个已认证的货主
        other_shipper = User.objects.create_user(
            phone='13800138004',
            password='testpass123',
            role=User.Role.SHIPPER,
            role_authstatus=True
        )
        
        self.client.force_authenticate(user=other_shipper)
        
        data = {
            'title': '更新后的货源',
            'weight': '150.00',
            'from_location': '广州天河',
            'to_location': '深圳南山',
            'loading_time': '2025-10-26T08:00:00Z'
        }
        
        response = self.client.put(f'/api/goods/{self.goods.id}/', data, format='json')
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
    
    def test_cannot_update_goods_status_through_update(self):
        """测试不能通过更新接口修改货源状态"""
        self.client.force_authenticate(user=self.shipper_user)
        
        data = {
            'title': '更新后的货源',
            'weight': '150.00',
            'from_location': '广州天河',
            'to_location': '深圳南山',
            'loading_time': '2025-10-26T08:00:00Z',
            'status': 'completed'  # 尝试修改状态
        }
        
        response = self.client.put(f'/api/goods/{self.goods.id}/', data, format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        
        # 验证状态没有被修改
        self.goods.refresh_from_db()
        self.assertEqual(self.goods.status, Goods.Status.PENDING)
        self.assertEqual(self.goods.title, '更新后的货源')
    
    def test_verified_shipper_can_create_goods(self):
        """测试已认证的货主可以创建货源"""
        self.client.force_authenticate(user=self.shipper_user)
        
        data = {
            'title': '新创建的货源',
            'weight': '200.00',
            'from_location': '北京',
            'to_location': '上海',
            'loading_time': '2025-10-27T08:00:00Z'
        }
        
        response = self.client.post('/api/goods/', data, format='json')
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        
        # 验证数据已创建
        self.assertEqual(Goods.objects.count(), 2)
        new_goods = Goods.objects.get(title='新创建的货源')
        self.assertEqual(new_goods.user, self.shipper_user)
    
    def test_unverified_shipper_cannot_create_goods(self):
        """测试未认证的货主不能创建货源"""
        self.client.force_authenticate(user=self.unverified_shipper_user)
        
        data = {
            'title': '新创建的货源',
            'weight': '200.00',
            'from_location': '北京',
            'to_location': '上海',
            'loading_time': '2025-10-27T08:00:00Z'
        }
        
        response = self.client.post('/api/goods/', data, format='json')
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
    
    def test_driver_cannot_create_goods(self):
        """测试车主不能创建货源"""
        self.client.force_authenticate(user=self.driver_user)
        
        data = {
            'title': '新创建的货源',
            'weight': '200.00',
            'from_location': '北京',
            'to_location': '上海',
            'loading_time': '2025-10-27T08:00:00Z'
        }
        
        response = self.client.post('/api/goods/', data, format='json')
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
    
    def test_admin_cannot_create_goods(self):
        """测试管理员不能创建货源"""
        self.client.force_authenticate(user=self.admin_user)
        
        data = {
            'title': '新创建的货源',
            'weight': '200.00',
            'from_location': '北京',
            'to_location': '上海',
            'loading_time': '2025-10-27T08:00:00Z'
        }
        
        response = self.client.post('/api/goods/', data, format='json')
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
